﻿using System;
using System.IO;
using System.Linq;

namespace MCO368
{
    static class ExtensionMethods
    {
        public static string HistogramString(this double[] list) 
        {
            String s = "";
            for (var i = 0; i < list.Length; i++)
                s += String.Format("{0} : {1:P2}\n",(char) (i + 'A'), list[i]);
            return s;
        }
    }
    public class Encryption
    {
        public static string HistogramString2(double[] list)
        {
            String s = "";
            for (var i = 0; i < list.Length; i++)
                s += String.Format("{0} : {1:P2}\n", (char)(i + 'A'), list[i]);
            return s;
        }

        public static String Encrypt(String s, int d)
        {
            return String.Join("", s.Select(c => Shift(Char.ToUpper(c), d)));
        }

        /// <summary>
        /// d must be positive
        /// </summary>
        /// <param name="c"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        static char Shift(char c, int d)
        {
            if (d < 0)
                d = d % 26 + 26;
            return Char.IsLetter(c) ? (char) ((c - 'A' + d) % 26 + 'A') : c;
        }

        static double[] Histogram(string s)
        {
            var h = new double[26];
            s.Where(Char.IsLetter)
             .Select(c => c - 'A').ToList()
             .ForEach(i => h[i]++);

            var total = h.Sum();
            for (int i = 0; i < h.Length; i++)
                h[i] /= total ;
            return h;
        }

        static void Main(string[] args)
        {
            //var text = "Hello";

            //foreach (char letter in text)
            //{
            //    Console.WriteLine(letter);
            //}


            //var text2 = String.Join("",
            //                        text.Select(c => Char.ToUpper(c))
            //                            .Select(c => ((char) (c + 1))));
 

            // Console.WriteLine(text2);

             //var text = File.ReadAllText(@"../../gba.txt");
             //Console.WriteLine(text);

             //var enc = Encrypt(text, 1);
             //Console.WriteLine(enc);

             //var dec = Encrypt(enc, -1);
             //Console.WriteLine(dec);

             //Console.WriteLine(HistogramString2(Histogram(dec)));
             //Console.WriteLine(Histogram(enc).HistogramString());

            IntToInt myfunction = foo;
            Console.WriteLine(myfunction(4));

            myfunction = (r => r + 1);
            Console.WriteLine(myfunction(4));

            DobToDob myfunction2;
            myfunction2 = (() => 1);


            Console.WriteLine(myfunction2());
            Console.ReadKey();
        }

        private delegate int IntToInt(int x);
        private delegate double DobToDob();

        private static int foo(int q)
        {
            return q + 1;
        }

    }
}
